package com.hk.aefz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hk.aefz.mapper.CommentMapper;
import com.hk.aefz.po.Comment;
import com.hk.aefz.po.UserInfo;
import com.hk.aefz.service.CommentService;
import com.hk.aefz.service.UserInfoService;
import com.hk.aefz.vo.CommentAdmin;
import com.hk.aefz.vo.PageResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author 阿尔法哲
 * @since 2020-07-21
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation("根据eid查询相关博客pl，并分页")
    @Override
    public PageResult<Comment> selectByEid(Integer eid , Integer page, Integer size) {
        PageResult<Comment> pageResult = new PageResult<>();
        Page<Comment> page1 = new Page<>(page,size); //分页
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("eid",eid)
                .orderByAsc("cmid");
        Page<Comment> commentPage = commentMapper.selectPage(page1, queryWrapper);
//        获取查询到的数据
        List<Comment> comments = commentPage.getRecords();
//        判断是否拥有评论
        if (!CollectionUtils.isEmpty(comments)) {
            //        获取aids 通过aid查询谁发表的评论
            List<Integer> aids = comments.stream().map(Comment::getAid).collect(Collectors.toList());
//            System.out.println("aids="+aids);
            List<UserInfo> userInfos = userInfoService.selectByIds(aids);
//        使用嵌套遍历进行二次封装赋值
            for (int i = 0; i < comments.size(); i++) {
                UserInfo userInfo = new UserInfo();
                for (int i1 = 0; i1 < userInfos.size(); i1++) {
                    userInfo = userInfos.get(i);
                    break;
                }
                comments.get(i).setUserInfo(userInfo);
            }
        }
//        获取总记录数
        long total = commentPage.getTotal();
//        获取总页数
        Long pages = 0l;
        if (total > 0l) {
            pages = total/size;
            if (total % size != 0) {
                pages = pages+1;
            }else {
                pages = pages;
            }
        }
        pageResult.setObjects(comments);
        pageResult.setTotal(total);
        pageResult.setPages(pages);
        pageResult.setSize((long)size); //强制转换
        pageResult.setCurrent(commentPage.getCurrent());
        return pageResult;
    }

    @Override
    public PageResult<CommentAdmin> selectList(Integer page, Integer size, int aid) {
        page = page*size; //下一页
        PageResult<CommentAdmin> pageResult = new PageResult<>();
        List<CommentAdmin> commentAdmins = commentMapper.selectArrayList(page, size, aid);
//        获取id 查询评论者昵称
        List<Integer> aids = commentAdmins.stream().map(CommentAdmin::getPlid).collect(Collectors.toList());
        List<UserInfo> userInfos = userInfoService.selectByIds(aids);
        List<String> unames = userInfos.stream().map(UserInfo::getUname).collect(Collectors.toList());
        //      数据库查询数据，会自动掉重复的数据，导致后期评论发布者赋值索引超出异常
        for (int i = 0; i < aids.size(); i++) {
            for (int i1 = 0; i1 < aids.size(); i1++) {
                Integer count = i1+1 >= aids.size() ? i1-1 : i1+1;
//                判断是否具有一个人评论多条
                if (aids.get(i) == aids.get(count)) {
//                    判断昵称是否小于ids总大小
                    if (unames.size() < aids.size()) {
//                        将当前id的昵称新增给unames昵称重复的索引位置
                        unames.add(count,unames.get(i));
                    } else continue;
                }else continue;
            }
        }
//        System.err.println(commentAdmins+"\t"+"评论者数量："+unames+"评论数量"+aids);
        for (int i = 0; i < commentAdmins.size(); i++) {
            for (int i1 = 0; i1 < unames.size(); i1++) {
                commentAdmins.get(i).setPlzname(unames.get(i));
                break; //赋值之后立即退出循环
            }
        }
        pageResult.setObjects(commentAdmins);
//        查询总pl记录数
        Long total = commentMapper.selectCount(aid);
        Long pages = 0l;
        if (total > 0l) {
            pages = total/size;
            if (total % size != 0l) {
                pages = pages +1;
            }else pages = pages;
        }
        pageResult.setTotal(total);
        pageResult.setPages(pages);
        pageResult.setSize((long) size); //强制转换
        pageResult.setCurrent((long)(page+1));
//        System.out.println("总记录"+pageResult);
        return pageResult;
    }

    @Override
    public int count(Integer eid) {
        int count = commentMapper.count(eid);
        return count;
    }
}
